<!DOCTYPE html>
<html lang="en">

	<head>
		<meta http-equiv="content-type" content="text/html; charset=UTF-8">
		<meta charset="utf-8">
		<title></title>
		<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1, user-scalable=no">
		<meta name="description" content="">
		<meta name="author" content="">
		<!-- STYLESHEETS -->
		<!--[if lt IE 9]><script src="../../js/flot/excanvas.min.js"></script><script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script><script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script><![endif]-->
    <link rel="stylesheet" type="text/css" href="../../css/cloud-admin.css" >
	<link rel="stylesheet" type="text/css"  href="../../css/themes/default.css" id="skin-switcher" >
	<link rel="stylesheet" type="text/css"  href="../../css/responsive.css" >	
	<link href="../../font-awesome/css/font-awesome.min.css" rel="stylesheet">
	<!-- SELECT2 -->
	<link rel="stylesheet" type="text/css" href="../../js/select2/select2.min.css" />
	<!-- UNIFORM -->
	<link rel="stylesheet" type="text/css" href="../../js/uniform/css/uniform.default.min.css" />
	<!-- datatable -->
    <link rel="stylesheet" href="../../js/datatables/datatable.css">
    <link rel="stylesheet" href="../../js/datepicker/datepicker.css">
       <link rel="stylesheet" type="text/css" href="../../js/file-input/fileinput.css"/>
  <script src="../../js/jquery/jquery-2.0.3.min.js"></script>
    <script src="../../js/common.js"></script>
	</head>

	<body>
		<header class="navbar clearfix" id="header">
		</header>

		<!-- PAGE -->
		<section id="page">
			<!-- SIDEBAR -->
			<div id="sidebar" class="sidebar">
				<div class="sidebar-menu nav-collapse">

					<!-- SIDEBAR MENU -->
					<ul>
						
					</ul>
					<!-- /SIDEBAR MENU -->
				</div>

			</div>
			<!-- /SIDEBAR -->
			<div id="main-content">

				<div class="container">
					<div class="row">
						<div id="content" class="col-lg-12">
							<!-- PAGE HEADER-->
							<div class="row">
								<div class="col-sm-12">
									<div class="page-header">
										<!-- STYLER -->

										<!-- /STYLER -->
										<!-- BREADCRUMBS -->
										<ul class="breadcrumb">
											<li>
												<i class="fa fa-home"></i>
												<a href="index.html">首页</a>
											</li>
											<li>
												<a href="#">商品管理</a>
											</li>
											<li>维护商品</li>
										</ul>
										<!-- /BREADCRUMBS -->

									</div>
								</div>
							</div>
							<!-- /PAGE HEADER -->
							<!-- SAMPLE -->
							
							<div class="row col-md-12">
								
							<form id="form_invoice">
								<input type="hidden" name="id"/>
								<div class="form-group  mrg-bt10" >
									<label class="control-label col-md-3" style="line-height:34px;"><span class="required">*</span>发票类型：</label>
									<div class="col-md-5" style="display: inline-flex">
										<select id="select_invoiceType" name="invoiceType" width="250px">
										<option value="VAT">增值税专用发票</option>
										<option value="General">普通发票</option>										
										</select>
									</div>
								</div>
								<div class="form-group  mrg-bt10" >
									<label class="control-label col-md-3" style="line-height:34px;"><span class="required">*</span>发票类型：</label>
									<div class="col-md-9" style="display: inline-flex">
										<select id="select_titleType" name="titleType" width="250px">
										<option value="Company">公司</option>		
										<option value="Person">个人</option>
																		
										</select>
									</div>
								</div>
								<div class="form-group  mrg-bt10">
									<label class="control-label col-md-3" style="line-height:34px;"><span class="required">*</span>发票抬头：</label>
									<div class="col-md-9" style="display: inline-flex">
										<input type="text" class="form-control"  style="width: 70%" name="invoiceTitle" placeholder="请输入抬头" maxlength="50" />
										
									</div>
								</div>
								<div class="form-group  mrg-bt10 company" style="display:none">
											<label class="control-label col-md-3"><span class="required">*</span>纳税人识别号：</label>
											<div class="col-md-9" style="text-align: left;display: inline-flex">
												<input type="text" class="form-control"  style="width: 70%" name="identificationNumber" placeholder="请输入纳税人识别号" maxlength="20" />										
											</div>

								</div>
								<div class="form-group  mrg-bt10 company" style="display:none;height: 100px;">
									<label class="control-label col-md-3" style="line-height:34px;height: 100px;"><span class="required">*</span>营业执照：</label>
									
									<div class="col-md-8 img" style="height: 100px;" >	
												
                                      	<div hidden-image ></div>
								          	<div class="fileinput fileinput-new" data-provides="fileinput" >
									          	<div class="image_preview_list" style="float:left;" id="imgs">
									          		<div class="fileinput-preview thumbnail image_preview_default" data-trigger="fileinput" style="width: 120px; height: 90px;"></div>	          		                           	
												</div>
												<div style="float:left; margin-left:5px; padding-top:23px;" class="addnewimg" id="f_span">
													<span class="btn btn-default btn-file" id="fir">
														<span class="fileinput-new" id="two" >上传图片</span>		
														<span class="fileinput-exists">Change</span>												
															<input type="file" name="uploadFile" id="businessImagePath" upload-file="uploadImage">
													</span>
													<div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" style=" margin-bottom: 0px;margin-top:10px;">
													<div class="progress-bar progress-bar-success" style="width:0%;"></div>
												    </div>	
												</div>
												
											</div>
																		 								
															
									</div>
							</div>
							<div class="form-group  mrg-bt10 company" style="display:none;height: 100px;">
									<label class="control-label col-md-3" style="line-height:34px;height: 100px;"><span class="required">*</span>一般纳税人：</label>
									
									<div class="col-md-8 img" style="height: 100px;" >	
													
                                      	<div hidden-image ></div>
								          	<div class="fileinput fileinput-new" data-provides="fileinput" >
									          	<div class="image_preview_list" style="float:left;" id="imgs">
									          		<div class="fileinput-preview thumbnail image_preview_default" data-trigger="fileinput" style="width: 120px; height: 90px;"></div>	          		                           	
												</div>
												<div style="float:left; margin-left:5px; padding-top:23px;" class="addnewimg" id="f_span">
													<span class="btn btn-default btn-file" id="fir">
														<span class="fileinput-new" id="two" >上传图片</span>		
														<span class="fileinput-exists">Change</span>												
															<input type="file" name="uploadFile" id="generalImagePath" upload-file="uploadImage">
													</span>
													<div class="progress progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" style=" margin-bottom: 0px;margin-top:10px;">
													<div class="progress-bar progress-bar-success" style="width:0%;"></div>
												    </div>	
												</div>
												
											</div>
																		 								
																	
									</div>
							</div>
								<div class="form-group  mrg-bt10 company" style="display:none" >
											<label class="control-label col-md-3"><span class="required">*</span>开户行及账号：</label>
											<div class="col-md-9" style="text-align: left;display: inline-flex">
												<input type="text" class="form-control"  style="width: 70%" name="bankAccount" placeholder="请输入开户行及账号" maxlength="50" />										
											</div>

								</div>
								<div class="form-group  mrg-bt10 company" style="display:none">
											<label class="control-label col-md-3"><span class="required">*</span>地址及电话：</label>
											<div class="col-md-9" style="text-align: left;display: inline-flex">
												<input type="text" class="form-control"  style="width: 70%" name="addressTel" placeholder="请输入地址及电话" maxlength="50" />										
									        </div>

								</div>
								<div class="form-group  mrg-bt10">
											<label class="control-label col-md-3"><span class="required">*</span>应开金额：</label>
											<div class="col-md-9" style="text-align: left;display: inline-flex">
												<input type="text" class="form-control"  style="width: 70%" name="invoiceMoney" onkeypress="return $.IsDecimal(event);" onblur="return $.IsMoney(this);" placeholder="请输入应开金额" maxlength="20" />										
											</div>

								</div>
		
								<div class="form-group  mrg-bt10">
											<label class="control-label col-md-3"><span class="required">*</span>申请人：</label>
											<div class="col-md-9" style="text-align: left;display: inline-flex">
												<input type="text" class="form-control" style="width: 70%" name="applyPerson" placeholder="请输入申请人" maxlength="10"/>												
											</div>

								</div>
								<div class="form-group  mrg-bt10 invoice" style="display:none">
											<label class="control-label col-md-3"><span class="required">*</span>发票号码：</label>
											<div class="col-md-9" style="text-align: left;display: inline-flex">
												<input type="text" class="form-control" style="width: 70%" name="invoiceNo" placeholder="请输入发票号码" maxlength="20"/>												
											</div>

								</div>
									<div class="form-group  mrg-bt10 invoice" style="display:none">
											<label class="control-label col-md-3"><span class="required">*</span>税额：</label>
											<div class="col-md-9" style="text-align: left;display: inline-flex">
												<input type="text" class="form-control" style="width: 70%" name="taxMoney" placeholder="请输入税额" onkeypress="return $.IsDecimal(event);" onblur="return $.IsMoney(this);" maxlength="10"/>												
											</div>

								</div>
									<div class="form-group  mrg-bt10 invoice" style="display:none">
											<label class="control-label col-md-3"><span class="required">*</span>税率：</label>
											<div class="col-md-9" style="text-align: left;display: inline-flex">
												<input type="text" class="form-control" style="width: 70%" name="taxRate" placeholder="请输入税率" onkeypress="return $.IsDecimal(event);" onblur="return $.IsMoney(this);" maxlength="10"/>												
											</div>

								</div>
								<div class="form-group  mrg-bt10 invoice" style="display:none">
											<label class="control-label col-md-3"><span class="required">*</span>开票日期：</label>
											<div class="col-md-9" style="text-align: left;display: inline-flex">
												<input type="text" class="form-control" style="width: 70%" name="invoiceDate" placeholder="请输入开票日期，如2017-06-06" maxlength="10"/>												
											</div>

								</div>
								<div class="form-group  mrg-bt10 cancelReason" style="display:none">
											<label class="control-label col-md-3">作废原因：</label>
											<div class="col-md-9" style="text-align: left;display: inline-flex">
												<input type="text" class="form-control" style="width: 70%" name="cancelReason"  maxlength="100"/>												
											</div>

								</div>
								<div class="form-group  mrg-bt10">
											<label class="control-label col-md-3"><span class="required">*</span>渠道：</label>
											<div class="col-md-9" style="text-align: left;display: inline-flex">
												<input type="text" class="form-control" style="width: 70%" name="sourceName" placeholder="请输入渠道" maxlength="30"/>												
											</div>

								</div>
								<div class="form-group  mrg-bt10">
									<label class="control-label col-md-3">订单号：</label>
									<div class="col-md-9" style="text-align: left;display: inline-flex">
										<input type="text" class="form-control" style="width: 70%" name="orderNos" placeholder="请输入订单号" maxlength="30"/>
									</div>
								</div>
								<div class="form-group  mrg-bt10">
									<label class="control-label col-md-3">接收邮箱：</label>
									<div class="col-md-9" style="text-align: left;display: inline-flex">
										<input type="text" class="form-control" style="width: 70%" name="receiveEmail" placeholder="请输入邮箱地址" maxlength="30"/>
									</div>
								</div>
								
								<div class="form-group  mrg-bt10">
											<label class="control-label col-md-3"><span class="required">*</span>发票明细：</label>
											<div class="col-md-9" style="text-align: left;display: inline-flex">
												<div><table id="importData" cellpadding="0" cellspacing="0" border="0" class="datatable table table-striped table-bordered table-hover" width="600px">
												<thead><tr>
												<td width="300px">名称</td>
												<td width="100px">单价</td>
												<td width="100px">数量</td>
												<td width="100px">单位</td>	
												
																					
												</tr></thead>
												
														<tbody>
														<tr>
														<td colspan="4">请导入明细</td>	
														</tr>
														</tbody>
													</table>
													</div>
													<div  style="width:10%;margin-left: 10px">
													<div class="box button_div">
														<a href="javascript:;" class="btn btn-success downloadBtn" style="width: 80px">
															下载模板
														</a>
														<span class="btn btn-warning  btn-file" id="fir">
															<span id="two">导入明细</span>
																<input name="uploadFile" upload-file="imageUrlProduct" type="file">
														</span>
														<a href="javascript:;" class="btn btn-success export" style="width: 80px;display:none">
															导出明细
														</a>
													</div>
											</div>									
																					
											</div>

								</div>
								
								<div class="form-group  mrg-bt10">
											<label class="control-label col-md-3">备注：</label>
											<div class="col-md-9" style="text-align: left;display: inline-flex">
												<input type="text" class="form-control" style="width: 70%" name="invoiceRemark" placeholder="备注" maxlength="100"/>												
											</div>

								</div>
								
														
							
							
								</form>
								<div class="row" style="height: 30px;">
								<div class="col-md-10" style="text-align: center;">
									<div class="box ">
										<a href="javascript:;"  class="btn btn-success config bt-submit" style="width: 100px;margin-top:20px;">
											保存
										</a>
										<a href="javascript:history.go(-1);"  class="btn btn-default" style="width: 100px;margin-top:20px;">
											取消并返回
										</a>
									</div>
								</div>
					         </div>			
						
							<!-- /SAMPLE -->

							<div class="footer-tools">
								<span class="go-top">
								<i class="fa fa-chevron-up"></i> Top
							</span>
							</div>
						</div>
						<!-- /CONTENT-->
					</div>
				</div>
			</div>
		</section>
		<!--/PAGE -->
		<!-- JAVASCRIPTS -->
		<!-- Placed at the end of the document so the pages load faster -->

		
	
	<!-- JQUERY -->
<script src="../../js/jquery/jquery-2.0.3.min.js"></script>
<script src="../../js/common.js"></script>
<!-- JQUERY UI-->
<script src="../../js/jquery-ui-1.10.3.custom/js/jquery-ui-1.10.3.custom.min.js"></script>
<!-- BOOTSTRAP -->
<script src="../../bootstrap-dist/js/bootstrap.min.js"></script>
<!-- SLIMSCROLL -->
<script type="text/javascript" src="../../js/jQuery-slimScroll-1.3.0/jquery.slimscroll.min.js"></script>
<script type="text/javascript" src="../../js/jQuery-slimScroll-1.3.0/slimScrollHorizontal.min.js"></script>
<!-- BLOCK UI -->
<script type="text/javascript" src="../../js/jQuery-BlockUI/jquery.blockUI.min.js"></script>
<!--bootbox-->
<script type="text/javascript" src="../../js/bootbox/bootbox.min.js"></script>
<!-- AUTOSIZE -->
<script type="text/javascript" src="../../js/autosize/jquery.autosize.min.js"></script>
<!-- spinner -->
<script src="../../js/spinner/spin.js"></script>
<!-- bootstrap-fileupload -->
<script type="text/javascript" src="../../js/jquery-upload/js/jquery.iframe-transport.js"></script>
<script type="text/javascript" src="../../js/jquery-upload/js/jquery.fileupload.min.js"></script>
<script type="text/javascript" src="../../js/jquery-upload/js/jquery.fileupload-process.min.js"></script>
<script type="text/javascript" src="../../js/jquery-upload/js/jquery.fileupload-validate.min.js"></script>

<!-- INPUT MASK -->
<script type="text/javascript" src="../../js/bootstrap-inputmask/bootstrap-inputmask.min.js"></script>

<!-- SELECT2 -->
<script type="text/javascript" src="../../js/select2/select2.min.js"></script>
<!-- UNIFORM -->
<script type="text/javascript" src="../../js/uniform/jquery.uniform.min.js"></script>
<!-- WIZARD -->
<script src="../../js/bootstrap-wizard/jquery.bootstrap.wizard.min.js"></script>
<!-- WIZARD -->
<script src="../../js/jquery-validate/jquery.validate.min.js"></script>
<script src="../../js/jquery-validate/additional-methods.min.js"></script>
<script src="../../js/bootstrap-wizard/form-wizard.js"></script>
<script language="javascript" type="text/javascript" src="../../js/My97DatePicker/WdatePicker.js"></script>
<!-- NESTABLE LISTS -->
<script type="text/javascript" src="../../js/nestable/jquery.nestable.min.js"></script>
<!-- COOKIE -->
<script type="text/javascript" src="../../js/jQuery-Cookie/jquery.cookie.min.js"></script>
<!-- CUSTOM SCRIPT -->
<script src="../../js/script.js"></script>

<script src="../../js/Sortable.min.js"></script>

<!-- DATA TABLES -->
<script type="text/javascript" src="../../js/datatables/bootstrap-select.js"></script>
<script type="text/javascript" src="../../js/datatables/datatable.js"></script>
<script type="text/javascript" src="../../js/datatables/datatable-bootstrap.js"></script>
<script type="text/javascript" src="../../js/datatables/datatable-tabletools.js"></script>
<script type="text/javascript" src="../../js/datatables/fnAddTr.js"></script>
	
   
		<script>
		
		var Source=function(){
	       
	        var id=null,flag=null;
	        var valiFrom=$("#form_invoice");
	        var formValiInit=function(){
	            valiFrom.validate({
	                doNotHideMessage: true,
					errorClass: 'error-span',
	                errorElement: 'span',
	                rules: {                	 
	                	invoiceTitle:{required:true},
	                	invoiceMoney:{required:true},
	                	identificationNumber:{required:true},
	                	applyPerson:{required:true},	                	
	                	sourceName:{required:true}
	                	
	                },
	                highlight: function (element) { 
	                    $(element).closest('.form-group').removeClass('has-success').addClass('has-error'); 
	                },
	
	                unhighlight: function (element) { 
	                    $(element).closest('.form-group').removeClass('has-error'); 
	                },	
	                success: function (label) {
	                   label.addClass('valid').closest('.form-group').removeClass('has-error').addClass('has-success'); 
	                    }
	               
				 });
			};
	        var getInvoice=function(){	          
	           $.ajax({
						type: "get",
						url: App.getContextPath()+"admin/invoice/getInvoice.do",
						async: true,
						data: {id:id},
						success: function(result) {
							if(result!=null){
								$("[name=id]").val(result.id);
								$("#select_invoiceType").select2("val",result.invoiceType);
								$("#select_titleType").select2("val",result.titleType);
                                $("[name=invoiceTitle]").val(result.invoiceTitle);
                                if(result.titleType === 'Person') {
                                    $("[name=invoiceTitle]").attr('readonly', 'readonly');
								}
								$("[name=invoiceMoney]").val(result.invoiceMoney);
								$("[name=orderNos]").val(result.orderNos);
								$("[name=applyPerson]").val(result.applyPerson);
								$("[name=identificationNumber]").val(result.identificationNumber);

								$("[name=bankAccount]").val(result.bankAccount);
								$("[name=addressTel]").val(result.addressTel);
								$("[name=sourceName]").val(result.sourceName);
								$("[name=invoiceRemark]").val(result.invoiceRemark);
								$("[name=invoiceDate]").val(result.invoiceDate);

								$("[name=taxMoney]").val(result.taxMoney);
								$("[name=taxRate]").val(result.taxRate);
								$("[name=invoiceNo]").val(result.invoiceNo);
								$("[name=receiveEmail]").val(result.receiveEmail);
								$("[name=orderNos]").val(result.orderNos);
								if(result.invoiceDate!=null)
								$("[name=invoiceDate]").val($.getLocalDate(result.invoiceDate));
								if(result.cancelReason&&result.cancelReason!=""){
									$("[name=cancelReason]").val(result.cancelReason);
									$(".cancelReason").show();
								}
								if(result.businessImagePath){
									$(".img:eq(0)").find("[hidden-image]").empty().append("<input type='hidden' name='businessImagePath' value='" + result.businessImagePath+"' />");
									$(".img:eq(0)").find(".image_preview_default").empty().html("<img src='"+result.businessImagePath+"' />");
								}
								if(result.generalImagePath){
									$(".img:eq(1)").find("[hidden-image]").empty().append("<input type='hidden' name='generalImagePath' value='" + result.generalImagePath+"' />");
									$(".img:eq(1)").find(".image_preview_default").empty().html("<img src='"+result.generalImagePath+"' />");
								}

								initDateTable(result.adminInvoiceDetails);
				        	
				        	 	$("#select_invoiceType").trigger("change");
				        	
								if(flag=="check"){
									$("table tbody").find("a").remove();
									$(".invoice").show();
									$("input").attr("readonly","readonly");
									$(".invoice").find("input").removeAttr("readonly");
									$(".button_div").find("a:eq(1)").show();
									$(".button_div").find("a:eq(0)").hide();
									$(".button_div").find("span:eq(0)").hide();

								}
								if(flag=="detail"){
									$(".btn-success").hide();
									$("input").attr("readonly","readonly");
									$(".invoice").show();
									$(".button_div").find("a:eq(1)").show();
									$(".button_div").find("a:eq(0)").hide();
									$(".button_div").find("span:eq(0)").hide();
									}
								if(flag!="edit") {
									$("select").attr("disabled","disabled").select2();
									$("table tbody").find("a").remove();

								}
							}

						}
				});
	        };
	        var isExistsInvoice=function(){
	        	var isExist=false;
	            $.ajax({
						type: "get",
						url: App.getContextPath()+"admin/invoice/isExistsInvoice.do",
						async: false,
						data: {invoiceNo:$("[name=invoiceNo]").val(),id:id},
						success: function(result) {
							if(result==true)
							{
							   $.alert("发票号码已经存在");
							   isExist=true;
							}
					     }
					   });
					return isExist;
	        };	      
	        //保存渠道
	        var saveInvoice=function(){	       
	        	if(valiFrom.valid() == false)return false;
	        	if(flag=="check"&&isExistsInvoice())
	        	 return false;
	        	var invoiceJson=$("#form_invoice").serializeObject();
                invoiceJson.invoiceType = $("#select_invoiceType").val();
                invoiceJson.titleType = $("#select_titleType").val();
	        	if(id!=null&&flag=="check"){
	        		if(invoiceJson.invoiceNo==""){
	        			$.alert("请输入发票号码");
	        			return false;
	        		}
	        		if(invoiceJson.taxMoney==""){
	        			$.alert("请输入税额");
	        			return false;
	        		}
	        		if(invoiceJson.invoiceDate==""){
	        			$.alert("请输入开票日期");
	        			return false;
	        		}
	        		if(invoiceJson.taxRate==""){
	        			$.alert("请输入税率");
	        			return false;
	        		}
	        		
	        	}
	        	$.blockUI();
	        	var detail=[];
	        	
	        	$("table tbody tr").each(function(){
	        		var item={};
	        		item.itemName=$(this).find("td:eq(0)").html();
	        		item.itemPrice=$(this).find("td:eq(1)").find("input").val();
	        		item.itemAmount=$(this).find("td:eq(2)").html();
	        		item.itemUnit=$(this).find("td:eq(3)").html();
	        		item.relationId="";
	        		detail.push(item);
	        	});
	        	
	        	
	        	/* if($("#importData").find("input").length>0){
	        		//获取发票明细		        	
		        	$("table tbody tr").each(function(){
		        		var item={};
		        		item.itemName=$(this).find("[name=itemName]").val();
		        		item.itemPrice=$(this).find("[name=itemPrice]").val();
		        		item.itemAmount=$(this).find("[name=itemAmount]").val();
		        		item.itemUnit=$(this).find("[name=itemUnit]").val();
		        		item.relationId=$(this).find("[name=relationId]").val();
		        		detail.push(item);
		        	});
	        	}else{
	        		var api = datetables.api();
		 	        var datas = api.rows().data();
		 	       for (var i = 0; i < datas.length; i++) {			        	 
		 	    	  detail.push(datas[i]);
		 	       }
	        	} */
	        	
	        	invoiceJson.adminInvoiceDetails=detail;
	        
	        	
	        	if(flag=="check")invoiceJson.invoiceStatus="Opend";
	        	$.confirm("确认保存发票?",function(){
	        		$.ajax({
						type: "post",
						url: App.getContextPath()+"admin/invoice/saveInvoice.do",
						async: true,
						data: $.param({invoice:JSON.stringify(invoiceJson)}),
						success: function(result) {
							$.unblockUI();
							if(result.type=="success")
							window.location.href="invoice_list.html";
					     }
					   });
	        		
	        	});
	        	
	        
	        };
	        var addRow=function(){
	        	var tr=$(this).parent().parent().html();
	        	var i=$("table tbody").find("tr").length;
	        	$("table tbody").append("<tr>"+tr+"</tr>");
	        	
	        	
	        	$("table tbody>tr:eq("+i+")").find(".add").html("删除").removeClass("add").removeClass("btn-primary").addClass("del").addClass("btn-danger");
	        	$("table tbody").find(".del").unbind("click").click(function(){
	        		$(this).parent().parent().remove();
	        	});
	        	
	        	
	        }
	        var initDateTable = function (data) {
	        	$("table tbody tr").remove();
	        	
	        	var tr="",totalmoney=0;
	        	$.each(data,function(index,item){
	        		tr+="<tr><td>"+item.itemName+"</td><td><input type=text value='"+item.itemPrice+"'/></td><td>"+item.itemAmount+"</td><td>"+item.itemUnit+"</td></tr>"
	        		$("table tbody").append(tr);
	        		tr="";
	        		totalmoney+=parseFloat(item.itemPrice)*item.itemAmount;
	        		if(index==data.length-1){
	        			totalmoney=parseFloat(totalmoney).toFixed(2);
	        			
	        			if(totalmoney!=Number($("[name=invoiceMoney]").val())){
	        				$.alert("导入的发票明细总金额"+totalmoney+"和应开金额不等");
	        				
	        			}
	        			
	        		}
	        	});
	            

	        }
	        var fileUpLoad=function(){	  
         		$(".progress").hide();       	
				$("[upload-file=uploadImage]").fileupload({
					url: App.getContextPath()+"admin/file/uploadImage.do",
					formData: {fileType: "Invoice",isFill:false},
					dataType: 'json',
					acceptFileTypes: /(\.|\/)(gif|jpe?g|png|JPG?g)$/i,
					iframe: true,
				    start:function(e) {		    	
				    	//$('.progress').fadeIn(300);					    	
				    	return true;
			    		},
					autoUpload: true,
					done: function(e, data) {
						var result = data.result;	
						if(result.message!=""){
							$.alert(result.message);							
							return false;
						}else{																
				    	var $tr = $(this).closest(".img");	
				    	var tag=$(this).attr("id");
				    	
				    	$tr.find("[hidden-image]").empty().append("<input type='hidden' name='"+tag+"' value='" + result.fileUrl+"' />");
				    	$tr.find(".image_preview_default").empty().html("<img src='"+result.fileUrl+"' />");			
						}
					},						
				    progressall: function (e, data) {					    	
			            var progress = parseInt(data.loaded / data.total * 100, 10);
			            $('.progress .progress-bar').css('width',progress + '%');
			        }
				}).on('fileuploaddone',function(e,data) {
						$(".progress").fadeOut(300);
				
				});
			
         };
         
	         return {	         
		         init:function(){		            
					 formValiInit();
					 fileUpLoad();
                     $("#select_invoiceType").attr('disabled','disabled').select2();
                     $("#select_titleType").attr('disabled','disabled').select2();
					 $("#select_titleType").change(function(){
						 if($(this).val()=="Company"){
							 $(".company").show();
						 }else{
							 $(".company").hide();
						 }
					 
					 });
					 $("#select_invoiceType").change(function(){
						if($(this).val()=="VAT"){
							$(".company").show();
							$(".invoice").hide();
							$("#select_titleType").val("Company");
						}else{
							$(".company").hide();
							//$(".invoice").show();
						}
						$("#select_titleType").trigger("change");
					 });
					 
					 $(".bt-submit").unbind("click").click(saveInvoice);
		             id=$.getUrlParam("id");
		             flag=$.getUrlParam("flag");
		             if(id){
		            	 
		            	 getInvoice();}
		             else{
		            	 $(".add").click(addRow);
		            	 $("#select_invoiceType").trigger("change");
		            	 
		            	
		             }
		           //下载
	                 $(".downloadBtn").click(function () {
	                     location.href = App.getContextPath() + "admin/file/downloadTemplate.do?template=InvoiceDetail";
	                 });
	               //导出明细
	                 $(".export").click(function () {
	                     location.href = App.getContextPath() + "admin/invoice/exportInvoiceDetail.do?id="+id;
	                 });
	               //导入模板
	                 $("[upload-file=imageUrlProduct]").fileupload({
	                     url: App.getContextPath() + "admin/invoice/readDetailTemplate.do",
	                     dataType: 'json',
	                     acceptFileTypes: /(\.|\/)(xlsx|xls)$/i,
	                     iframe: true,
	                     start: function (e) {
	                     	$.blockUI();
	                         $('.progress').fadeIn(300);
	                         return true;
	                     },
	                     autoUpload: true,
	                     done: function (e, data) {
	                     	$.unblockUI();
	                         if (data.result.type == 'error') {
	                             $.alert("导入的数据有问题");
	                         } else {
	         					
	         					initDateTable(data.result);
	                         }

	                     },
	                     progressall: function (e, data) {
	                     	
	                         var progress = parseInt(data.loaded / data.total * 100, 10);
	                         $('.progress .progress-bar').css('width', progress + '%');
	                     }
	                 }).on('fileuploaddone', function (e, data) {
	                     $(".progress").fadeOut(300);
	         			
	                 });
	               
		             
		             
		             
		          }
		    }
	      }();
	      
	      $(function(){
	      	$("#header").load("../head.html");
	        App.init(); //Initialise plugins and elements	        
	      	Source.init();
	      	
	      });
		</script>
	

		<!-- /JAVASCRIPTS -->
	</body>

</html>